package com.ricky04.ApplicationContext;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Iterator;

/**
 * 一种是springapplication.addInitializers
 * 一种是配置context.initializer.classes=com.ricky04.ApplicationContext.MyApplicationContextInitializer
 * 一种是通过spring.factories中配置org.springframework.context.ApplicationContextInitializer=com.ricky04.ApplicationContext.MyApplicationContextInitializer
 */
public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        System.out.println("Bean的数量是："+configurableApplicationContext.getBeanDefinitionCount());
        ConfigurableListableBeanFactory beanFactory = configurableApplicationContext.getBeanFactory();
        Iterator<String> beanNamesIterator = beanFactory.getBeanNamesIterator();
        while (beanNamesIterator.hasNext()){
            System.out.print(beanNamesIterator.next()+",");
        }
    }
}
